/**
 * 功能描述
 * 服务器交
 */

define(function (require, exports, module) {
    var aw = require("ajaxwrapper");
    var Dialog = require("dialog");
    
    module.exports = {
    	save:function(formData,gridData,totalGridData,callback){
    		if(isNaN(totalGridData[0].receiptsTotal)){
    			Dialog.alert({
            		title:"提示",
            		content:"请填写实收金额合计(不能为空,且只能输入数字)！"
            	});
    			return false;
    		}
    		var paymentMethods = [];
    		if(totalGridData[0].paymentMethod.length == 0){
    			Dialog.alert({
            		title:"提示",
            		content:"请填写结算方式！"
            	});
    			return false;
    		}else{
    			for(var i=0; i < totalGridData[0].paymentMethod.length; i++){
    				paymentMethods[i] = totalGridData[0].paymentMethod[i].key;
    			}
    		}
    		
    		var pkUser = formData["allianceSettlement.operator.pkUser"]?formData["allianceSettlement.operator.pkUser"]:formData["pkUser"];
    		
    		var data = {
				"allianceSettlement.pkAllianceSettlement": formData["allianceSettlement.pkAllianceSettlement"],
				"allianceSettlement.version": formData["allianceSettlement.version"]?formData["allianceSettlement.version"]:0,
				"allianceSettlement.allianceConfirm": formData["allianceConfirm.pkAllianceConfirm"],
				"allianceSettlement.operator": pkUser,
				"allianceSettlement.settlementDate": formData["allianceSettlement.settlementDate"],
				"allianceSettlement.balance":parseFloat(totalGridData[0].receiptsTotal),
				"allianceSettlement.paymentMethods":paymentMethods,
				"allianceSettlement.description":totalGridData[0].description
    		};
    		
    		var asdList = [];
    		for(var i=0;i<gridData.length;i++){
    			var idata = gridData[i];
    			var finish = idata.finish;
    			if(typeof idata.finish == "object"){
    				finish = idata.finish.key;
    			}
    			asdList.push({
    				pkAllianceSetlDetail: idata.pkAllianceSetlDetail,
    				signUpCharge: idata.signUpCharge.pkSignUpCharge,
    				allianceCharges: idata.allianceCharges,
    				manageCharges: idata.manageCharges,
    				otherCharges: idata.otherCharges,
    				deductionCharges: idata.deductionCharges,
    				finish: finish,
    				version: idata.version
    			});
    		}
    		data.asdList = asdList;
    		data = aw.customParam(data)
    		aw.saveOrUpdate("api/alliancesettlement/savesetl",data,function(data){
    			callback(data);
			});
    	}
    };
});
